import { reactive } from "vue";
import { U16, U32 } from "./codec";
import { Message } from "./message";
import { hex4 } from "./ui";

export class MainSwSensor {

    is_heat_open: boolean;
    is_heat_close: boolean;
    is_updown_alm: boolean;
    is_updown_origin: boolean;
    is_updown_origin2: boolean;
    is_rotate_alm: boolean;
    is_rotate_origin: boolean;
    is_pasta_open: boolean;
    is_pasta_close: boolean;
    is_extern_push_alm: boolean;
    is_extern_push_front: boolean;
    is_extern_push_back: boolean;
    is_pasta_check: boolean;
    is_pick_open: boolean;
    is_pick_close: boolean;
    is_pick_alm: boolean;
    is_pick_prot1: boolean;
    is_pick_prot2: boolean;
    is_pick_raster: boolean;
    is_fetch_alm: boolean;
    is_tray_ok: boolean;
    is_track_ok: boolean;
    is_cargo_btn: boolean;
    is_door_close: boolean;
    is_fridge_close: boolean;
    
    constructor(value: U32) {
        this.is_heat_open = value.isBitSet(0);
        this.is_heat_close = value.isBitSet(1);
        this.is_updown_alm = value.isBitSet(2);
        this.is_updown_origin = value.isBitSet(3);
        this.is_updown_origin2 = value.isBitSet(4);
        this.is_rotate_alm = value.isBitSet(5);
        this.is_rotate_origin = value.isBitSet(6);
        this.is_pasta_open = value.isBitSet(7);
        this.is_pasta_close = value.isBitSet(8);
        this.is_extern_push_alm = value.isBitSet(9);
        this.is_extern_push_front = value.isBitSet(10);
        this.is_extern_push_back = value.isBitSet(11);
        this.is_pasta_check = value.isBitSet(12);
        this.is_pick_open = value.isBitSet(13);
        this.is_pick_close = value.isBitSet(14);
        this.is_pick_alm = value.isBitSet(15);
        this.is_pick_prot1 = value.isBitSet(16);
        this.is_pick_prot2 = value.isBitSet(17);
        this.is_pick_raster = value.isBitSet(18);
        this.is_fetch_alm = value.isBitSet(19);
        this.is_tray_ok = value.isBitSet(20);
        this.is_track_ok = value.isBitSet(21);
        this.is_cargo_btn = value.isBitSet(22);
        this.is_door_close = value.isBitSet(23);
        this.is_fridge_close = value.isBitSet(24);
    }
}

export interface MainState {
    version: string,
    sw_sensor: MainSwSensor,
    track: number,
    tray: number,
    fetch_position: number,
}

export const mainState = reactive<MainState>({
    version: '',
    sw_sensor: new MainSwSensor(new U32()),
    track: 0,
    tray: 0,
    fetch_position: 0,
});

export function onMainState(msg: Message) {
    const version = new U16();
    const sw = new U32();
    const track = new U16();
    const tray = new U16();
    const fetch_position = new U16();

    msg.parseResult(version, sw, track, tray, fetch_position);

    mainState.version = hex4(version.value);
    mainState.sw_sensor = new MainSwSensor(sw);
    mainState.track = track.signedValue();
    mainState.tray = tray.signedValue();
    mainState.fetch_position = fetch_position.value;
}

